package practise;

import java.util.List;
import java.util.Stack;

public class EvalRPN {
    public static int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        int n1,n2;
        for (int i = 0; i < tokens.length; i++) {
            String key = tokens[i];
            switch (key) {
                case "+" :
                    stack.push(String.valueOf((Integer.parseInt(stack.pop()) + Integer.parseInt(stack.pop()))));
                    break;
                case "-" :
                    n1 = Integer.parseInt(stack.pop());
                    n2 = Integer.parseInt(stack.pop());
                    stack.push(String.valueOf( n2 - n1));
                    break;
                case "*" :
                    stack.push(String.valueOf((Integer.parseInt(stack.pop()) * Integer.parseInt(stack.pop()))));
                    break;
                case "/" :
                    n1 = Integer.parseInt(stack.pop());
                    n2 = Integer.parseInt(stack.pop());
                    stack.push(String.valueOf( n2 / n1));
                    break;
                default:
                    stack.push(key);
                    break;
            }
        }
        return Integer.parseInt(stack.pop());
    }

    public static void main(String[] args) {
        String[] arr = new String[]{"4","13","5","/","+"};
        System.out.println(evalRPN(arr));
    }
}
